<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button size="small" type="primary" @click="onAdd(null)" v-hasPermi="['platform:user:tag:save']">{{
          $t('tianJiaYongHuBiaoQian')
        }}</el-button>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="small">
        <el-table-column label="ID" min-width="80" prop="id" />
        <el-table-column :label="$t('biaoQianMingCheng')" min-width="180">
          <template slot-scope="{ row }">
            <span v-text="$route.path.indexOf('group') !== -1 ? row.groupName : row.name"></span>
          </template>
        </el-table-column>
        <!--<el-table-column-->
        <!--prop="create_time"-->
        <!--label="创建时间"-->
        <!--min-width="150"-->
        <!--/>-->
        <el-table-column :label="$t('caoZuo')" min-width="120" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              class="mr10"
              type="text"
              size="small"
              @click="onAdd(scope.row)"
              v-hasPermi="['platform:user:tag:update']"
              >{{ $t('bianJi') }}</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.row.id, scope.$index)"
              disable
              v-hasPermi="['platform:user:tag:delete']"
              >{{ $t('shanChu') }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { tagListApi, tagDeleteApi, tagSaveApi, tagUpdateApi } from '@/api/user';
export default {
  name: 'UserGroup',
  data() {
    return {
      tableFrom: {
        page: 1,
        limit: 20,
      },
      tableData: {
        data: [],
        total: 0,
      },
      listLoading: true,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    info() {},
    onAdd(row) {
      this.$prompt(this.$t('biaoQianMingCheng'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        inputErrorMessage: this.$t('qingShuRuBiaoQianMingCheng'),
        inputType: 'text',
        closeOnClickModal: false,
        inputValue: row ? row.name : '',
        inputPlaceholder: this.$t('qingShuRuBiaoQianMingCheng'),
        inputValidator: (value) => {
          if (!value) return this.$t('shuRuBuNengWeiKong');
        },
      })
        .then(({ value }) => {
          row
            ? tagUpdateApi(row.id, { name: value }).then(() => {
                this.$message.success(this.$t('bianJiChengGong'));
                localStorage.removeItem('tagAllList');
                this.getList();
              })
            : tagSaveApi({ name: value }).then(() => {
                this.$message.success(this.$t('xinZengChengGong'));
                localStorage.removeItem('tagAllList');
                this.getList();
              });
        })
        .catch(() => {
          this.$message.info(this.$t('quXiaoShuRu'));
        });
    },
    // 列表
    getList() {
      this.listLoading = true;
      tagListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          localStorage.setItem('levelKey', res.list);
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    // 删除
    handleDelete(id, idx) {
      this.$modalSure(this.$t('tishi18')).then(() => {
        tagDeleteApi(id).then(() => {
          this.$message.success(this.$t('shanChuChengGong'));
          localStorage.removeItem('tagAllList');
          this.tableData.data.splice(idx, 1);
        });
      });
    },
  },
};
</script>

<style scoped lang="stylus"></style>
